package com.newly.common.base.constants;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * 权限常量
 */
public interface AuthConstant {
    /**
     * oauth2用户名
     */
    String OAUTH2_CLIENT_ID = "d0a492b7bcae45efb769a6f7ee1f3054";
    /**
     * oauth2密码
     */
    String OAUTH2_SECRET_ID = "188a9e58c0cc45b9a6c731e33180df10";
    /**
     * oath2登录类型
     */
    String OAUTH2_GRANT_TYPE = "password";
    /**
     * oath2登录范围
     */
    String OAUTH2_SCOPES = "all";
    /**
     * oath2登录跳转url
     */
    String OAUTH2_REDIRECT_URLS = "http://www.baidu.com";
    /**
     * oauth2 token颁发有效时长
     */
    Integer OAUTH2_EXPIRE_TIME = 3600 * 10;
    /**
     * 登录用户类型
     */
    String LOGIN_USER_TYPE = "User-Type";
    /**
     * jwt签名key
     */
    String JWT_SIGNING_KEY = "newly-jwt-siging-key";
    /**
     * token
     */
    String JWT_TOKEN_KEY = "Authorization";
    String JWT_TOKEN_SUFFIX_KEY = "bearer "; // 注意不能去掉空格
    String JWT_TOKEN_SUFFIX_KEY_2 = "Bearer "; // 注意不能去掉空格
    /**
     * spring-security角色要求的前缀
     */
    String ROLE_PREFIX = "role_";
    /**
     * 顶级父角色标识
     */
    Integer ROLE_PARENT_FLAG = 0;

    /**
     * 用户类型
     */
    enum UserType {
        ADMIN(0, "管理员"),
        USER(1, "普通用户"),
        DEV(-1, "超级管理员");

        UserType(int code, String remark) {
            this.code = code;
            this.remark = remark;
        }

        private int code;
        private String remark;

        public int getCode() {
            return code;
        }

        public String getRemark() {
            return remark;
        }

        public static UserType getInstance(int code) {
            for (UserType value : UserType.values()) {
                if (value.code == code) {
                    return value;
                }
            }
            return null;
        }
    }

    /**
     * 角色类型
     */
    enum RoleType {
        MANAGER(1000, "管理端"),
        CLIENT(2000, "客户端");

        RoleType(int code, String remark) {
            this.code = code;
            this.remark = remark;
        }

        private int code;
        private String remark;

        public int getCode() {
            return code;
        }

        public String getRemark() {
            return remark;
        }

    }

    /**
     * 权限实体
     */
    @JsonFormat(shape = JsonFormat.Shape.OBJECT)
    enum AuthEntityType {
        MENU(10000, "menu", "菜单权限"),
        TENANT(20000, "tenant", "租户权限"),
        DOMAIN(30000, "domain", "区域权限"),
        SITE(2, "site", "站点权限"),
        COMPANY(3, "company", "企业权限"),
        DEVICE(50000, "device", "设备权限");

        AuthEntityType(int code, String name, String remark) {
            this.code = code;
            this.name = name;
            this.remark = remark;
        }

        AuthEntityType() {
        }

        private int code;
        private String name;
        private String remark;

        public static AuthEntityType getInstance(Integer typeCode) {
            if (ObjectUtil.isEmpty(typeCode)) return null;

            for (AuthEntityType value : AuthEntityType.values()) {
                if (typeCode.equals(value.getCode())) {
                    return value;
                }
            }
            return null;
        }

        public int getCode() {
            return code;
        }

        public String getName() {
            return name;
        }

        public String getRemark() {
            return remark;
        }
    }

    /**
     * 启用状态
     */
    @JsonFormat(shape = JsonFormat.Shape.OBJECT)
    enum Enable {
        OPEN(1, "开启"),
        CLOSE(0, "关闭");

        Enable(int code, String remark) {
            this.code = code;
            this.remark = remark;
        }

        private int code;
        private String remark;

        public static AuthEntityType getInstance(Integer enable) {
            for (AuthEntityType value : AuthEntityType.values()) {
                if (enable.equals(value.getCode())) {
                    return value;
                }
            }
            return null;
        }

        public int getCode() {
            return code;
        }

        public String getRemark() {
            return remark;
        }
    }
}
